package com.jianghu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jianghu.helpers.GetLineFromtxt;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MyRecommend extends Activity {

	// recommend list view;
	private ListView listView;
	View loadingLayout;
	SpecialAdapter listItemAdapter;
	ArrayList<HashMap<String, Object>> listItem;
	private String user;
	public Context context = MyRecommend.this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myrecommend);
		Intent intent = this.getIntent();
		user = intent.getStringExtra("item").toString();
		ImageButton home = (ImageButton) findViewById(R.id.homebuttonofusertrends);
		home.setOnClickListener(new homeClickListener());
		ImageButton refresh = (ImageButton) findViewById(R.id.refreshbuttonofusertrends);
		refresh.setOnClickListener(new refreshClickListener());

		listView = (ListView) findViewById(R.id.listViewOfmyrecommend);
		loadingLayout = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_listview_user_trends, null, false);
		listView.addFooterView(loadingLayout, null, true);

		init();
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.setClass(MyRecommend.this, Article.class);

				MyRecommend.this.startActivity(intent);
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		listItemAdapter =new SpecialAdapter(this, listItem, listView);
		listView.setAdapter(listItemAdapter);
	}

	private void init() {
		listItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 20; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			String str = GetLineFromtxt.readAppointedLineNumber(this, i + 1);
			map.put("user", "设计工房");
			map.put("userimage", "abc");
			map.put("time", "05-07 08:45");
			map.put("title", "标题标题标题标题");
			map.put("content", str);
			map.put("typeimage", "abc");
			listItem.add(map);
		}
	}

	class homeClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MyRecommend.this, MainPage.class);
			MyRecommend.this.startActivity(intent);
		}

	}

	class refreshClickListener implements OnClickListener {
		private ProgressDialog progressDialog;

		@Override
		public void onClick(View v) {

			progressDialog = ProgressDialog.show(MyRecommend.this, "刷新中..",
					"请稍等...", true, false);
			new Thread() {

				@Override
				public void run() {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					handlerRefresh.sendEmptyMessage(0);
				}
			}.start();
		}

		private Handler handlerRefresh = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				listView.setSelection(0);
				progressDialog.dismiss();
			}
		};
	}

	public class SpecialAdapter extends BaseAdapter {
		private ArrayList<HashMap<String, Object>> list;
		private Activity activity;
		public SpecialAdapter(Activity activity,
				ArrayList<HashMap<String, Object>> list, ListView listView) {
			this.activity=activity;
			this.list=list;
		}

		public class Holer{
			TextView user,title,time,content;
			ImageView userimage,typeimage;
			
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			View view=convertView;
			Holer holder;
			if (view == null) {
				LayoutInflater inflater = activity.getLayoutInflater();
				view = inflater.inflate(R.layout.listview_myrecommend_trends,
						null);
				holder =new Holer();
				holder.user=(TextView)view.findViewById(R.id.myreco_user);
				holder.title=(TextView)view.findViewById(R.id.myreco_title);
				holder.time=(TextView)view.findViewById(R.id.myreco_time);
				holder.content=(TextView)view.findViewById(R.id.myreco_content);
				holder.userimage=(ImageView)view.findViewById(R.id.myreco_usericon);
				holder.typeimage=(ImageView)view.findViewById(R.id.myreco_type);
				view.setTag(holder);
			} else {
				holder = (Holer)convertView.getTag();
			}
			final HashMap<String, Object> map = list.get(position);
			if (map!=null) {
				holder.user.setText((String)map.get("user"));
				holder.title.setText((String)map.get("title"));
				holder.time.setText((String)map.get("time"));
				holder.content.setText((String)map.get("content"));
			}
			return view;

		}
		@Override
		public int getCount() {
			return list.size();
		}
		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}

	}

}
